package bjtuoj;

import java.util.Scanner;

/**
 * ClassName: Homework_3_F
 * Description:
 * date: 2021-11-09 10:32
 *
 * @author liyifan
 */
public class Homework_3_F {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        while (N-- > 0) {
            int[] dp = new int[500 + 10];
            int l_a = scanner.nextInt();
            int[] a = new int[l_a];
            for (int i = 0;i < l_a;i++){
                a[i] = scanner.nextInt();
            }
            int l_b = scanner.nextInt();
            int[] b = new int[l_b];
            for (int i = 0;i < l_b;i++) {
                b[i] = scanner.nextInt();
            }

            int res = 0;
            for (int i = 0;i < l_a;i++) {
                int mx = 0;
                for (int j = 0;j < l_b;j++) {
                    if (b[j] < a[i])
                        mx = Math.max(mx, dp[j]);
                    if (b[j] == a[i])
                        dp[j] = mx + 1;
                    res = Math.max(res, dp[j]);
                }
            }
            System.out.printf("%d\n", res);
        }
    }
}
